package com.gopay.checkorder.service.task;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gopay.checkorder.service.task.thread.CheckMainOrderAccountThread;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.cps.manager.order.CpsGeneralMainOrderQueryManager;
import com.gopay.common.domain.cps.CpsGenMainOrder;

/**
 * 订单状态同步任务
 * 
 * @CheckMainOrderAccountTaskImpl.java
 * @author yaobo
 * @2013-7-10 下午03:29:42 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("checkMainOrderAccountTask")
public class CheckMainOrderAccountTaskImpl implements Task {
    private final static Logger logger = LoggerFactory.getLogger("ORDER_ACCOUNT_SYNC");

    private final static int MAX_SIZE = 2000;

    @Resource(name = "cpsGeneralMainOrderQueryManager")
    private CpsGeneralMainOrderQueryManager mainOrderQuery;

    @Resource(name = "checkMainOrderAccountThread")
    private CheckMainOrderAccountThread checkMainOrderAccountThread;

    @Override
    public void doTask() {
        int pageSize = 100;
        int pageIndex = 1;
        int count = 0;

        int txnStaCd = TxnStaCode.TXN_STA_CD_30000.value;
        int resCount = mainOrderQuery.getMainOrderCount(txnStaCd);
        if (resCount > MAX_SIZE) {
            resCount = MAX_SIZE;
        }

        List<CpsGenMainOrder> mainOrders = null;
        while (count < resCount) {
            mainOrders = mainOrderQuery.getMainOrders(txnStaCd, pageIndex, pageSize);
            if (mainOrders == null || mainOrders.size() == 0) {
                logger.error("无进行中订单");
            }

            // 多线程下执行状态同步
            checkMainOrderAccountThread.run(mainOrders);

            count += pageSize;
            pageIndex++;
        }
    }
}
